import 'dart:convert';

import 'package:flutter/material.dart';

import 'base_tools.dart';
import 'list_type.dart';

/// 动画 or 图片
enum CurtainLightDiyType { animation, photo }

/// 固定[fixed], 上移[moveUp], 下移[moveDown],
/// 左移[moveLeft], 右移[moveRight], 闪烁[flicker]
enum CurtainLightDiyExecutionType {
  fixed,
  moveUp,
  moveDown,
  moveLeft,
  moveRight,
  flicker
}

/// 设备方向
enum DiyDeviceDirectionType { horizontal, vertical }

/// 播放方式
enum SettingsType { loop, toAndFro }

class CurtainLightDiyItem {
  CurtainLightDiyItem({
    required this.amount,
    required this.diyType,
    required this.directionType,
    required this.dx,
    required this.dy,
    required this.colorList,
    required this.speed,
    required this.brightness,
    required this.settingsType,
    this.listType = ListType.normal,
  });

  late int amount;
  late CurtainLightDiyType diyType;
  late DiyDeviceDirectionType directionType;
  late int dx;
  late int dy;
  late List<List<Color>> colorList;
  late int speed;
  late int brightness;
  late SettingsType settingsType;
  late ListType listType;

  CurtainLightDiyItem.fromJson(Map<String, dynamic> json) {
    amount = json['amount'];
    diyType =
        BaseTools.enumFromString(CurtainLightDiyType.values, json['diyType'])!;
    directionType = BaseTools.enumFromString(
        DiyDeviceDirectionType.values, json['directionType'])!;
    dx = json['dx'];
    dy = json['dy'];
    colorList = List.from(json['colorList']).map((e) {
      e as List;
      return e.map((e) => Color(e)).toList().cast<Color>();
    }).toList();
    speed = json['speed'];
    brightness = json['brightness'];
    settingsType =
        BaseTools.enumFromString(SettingsType.values, json['settingsType'])!;
    listType = BaseTools.enumFromString(ListType.values, json['listType'])!;
  }



  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = Map<String, dynamic>();
    List<List<int>> list = [];
    for (int i = 0; i < colorList.length; i++) {
      List<int> c = colorList[i].map((e) => e.value).toList();
      list.add(c);
    }
    data['amount'] = amount;
    data['diyType'] = BaseTools.enumToString(diyType);
    data['directionType'] = BaseTools.enumToString(directionType);
    data['dx'] = dx;
    data['dy'] = dy;
    data['colorList'] = list;
    data['speed'] = speed;
    data['brightness'] = brightness;
    data['settingsType'] = BaseTools.enumToString(settingsType);
    data['listType'] = BaseTools.enumToString(listType);
    return data;
  }
}

enum CurtainLightMusicPresetType { preset, diy }

// class CurtainLightMusicPresetItem {
//   /// 内置 or DIY
//   late CurtainLightMusicPresetType musicType;
//
//   /// 颜色列表
//   late List<DiyModule> colorList;
//
//   /// 色彩动态
//   late ColorDynamicType dynamicType;
//
//   /// 落点
//   late DropPointType dropPointType;
//
//   /// 落点颜色
//   HSVColor? dropPointHSV;
//
//   /// 方向
//   late CurtainDirectionType directionType;
//
//   CurtainLightMusicPresetItem.fromJson(Map<String, dynamic> json) {
//     musicType = BaseTools.enumFromString(
//         CurtainLightMusicPresetType.values, json['musicType'])!;
//     colorList = List.from(json['colorList'])
//         .map((e) => DiyModule.fromJson(jsonDecode(e)))
//         .toList();
//     dynamicType =
//         BaseTools.enumFromString(ColorDynamicType.values, json['dynamicType'])!;
//     dropPointType =
//         BaseTools.enumFromString(DropPointType.values, json['dropPointType'])!;
//     dropPointHSV = json['dropPointHSV'] != null
//         ? HSVColor.fromColor(Color(json['dropPointHSV']))
//         : null;
//     directionType = BaseTools.enumFromString(
//         CurtainDirectionType.values, json['directionType'])!;
//   }
//
//   Map<String, dynamic> toJson() {
//     final Map<String, dynamic> data = Map<String, dynamic>();
//     data['musicType'] = BaseTools.enumToString(musicType);
//     data['colorList'] = colorList.map((e) => jsonEncode(e.toJson())).toList();
//     data['dynamicType'] = BaseTools.enumToString(dynamicType);
//     data['dropPointType'] = BaseTools.enumToString(dropPointType);
//     data['dropPointHSV'] = dropPointHSV?.toColor().value ?? null;
//     data['directionType'] = BaseTools.enumToString(directionType);
//     return data;
//   }
//
// }
